package cn.hicard.risk.interf.bankcall;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;

import cn.hicard.risk.common.Constant;
import cn.hicard.risk.interf.tools.JsonPrintUtil;
import cn.hicard.risk.interf.tools.Post;
import cn.hicard.risk.interf.tools.UrlUtil;
import cn.hicard.risk.interf.vo.AppHead;
import cn.hicard.util.DateHelp;


public class BankcallTransInfoInterf {

	private static Logger logger = Logger.getLogger(Constant.LOGGER_MAIN);
	
	public List<BankcallTransInfoVO> seachTransInfo(BankcallTransInfoVO vo,String transType) throws Exception{
		List<BankcallTransInfoVO> infoList = new ArrayList<BankcallTransInfoVO>();
		BankcallTransInfo sendBean = new BankcallTransInfo();
		
		AppHead appHead = new AppHead();
		appHead.setTransType(transType);
		appHead.setSerialNo(DateHelp.dateNumber());
		appHead.setDataTime(DateHelp.dateTime());
		sendBean.setAppHead(appHead);
		sendBean.setAppbody(vo);
		
		try {
			RetBean retBean = assemblingMessage(sendBean,"seachTransInfo");
			if(retBean!=null){
				String retCode = transType+"_00";
				if(retBean.getRetCode()==null||!retCode.equals(retBean.getRetCode())){
					logger.error("风控与清算系统接口[seachTransInfo]失败，原因:"
							+ retBean.getRetMessage());
					throw new Exception();
				}else{
					infoList = retBean.getAppbody();
					
				}
			}else{
				logger.error("风控与清算系统接口[seachTransInfo]失败");
				throw new Exception();
			}
			
			
		} catch (Exception e) {
			logger.error("风控与清算系统接口[seachTransInfo]失败");
			throw new Exception();
		}
		return infoList;
	}
	 
	
	/**
	 * 客户端发起json数据请求
	 * 
	 * @param vo
	 * @return
	 * @throws Exception
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	public RetBean assemblingMessage(BankcallTransInfo vo, String funName)
			throws Exception {
		String sJson = ""; // 发送数据
		String backStr = ""; // 接收清算返回数据
		RetBean retBean = null;

		UrlUtil urlutil = new UrlUtil();
		String url = urlutil.getUrl();// 获取清算接口地址

		try {

			JsonPrintUtil json = new JsonPrintUtil();// 转换为JSON格式
			sJson = json.objectToJson(vo);
			logger.info("调用清算接口" + funName + "，风控原始请求数据为：[" + sJson + "]");
			Post hPost = new Post(url, sJson);
			backStr = new String(hPost.postjson(url, sJson).getBytes("gbk"));
			// hPost.start();// 启动线程
			logger.info("清算返回的json为：[" + backStr + "]");

			ObjectMapper mapper = new ObjectMapper();
			retBean = mapper.readValue(backStr, RetBean.class);

		} catch (Exception e) {

			logger.error("风控与清算系统接口[assemblingMessage]发生异常", e);
			throw new Exception(e.getMessage());
		}
		return retBean;
	}
	
	public static void main(String[] args) {
		BankcallTransInfoInterf intef = new BankcallTransInfoInterf();
		BankcallTransInfoVO vo = new BankcallTransInfoVO();
		vo.setAuthNo("");
		vo.setSysNo("");
		//vo.setOrderId("00001102030512301541");
		vo.setOrderId("xxxxxxxxxx");
		//vo.setOrderId("0000001111");
		//vo.setHmId("10440179980002");
		vo.setHmId("");
		//vo.setOmId("");
		//vo.setHmName("");
		//vo.setReckAmount(0);
		//vo.setTransAmount(0);
		//vo.setOrgNo("");
		
		String transType = "F1003";
		try {
			List<BankcallTransInfoVO> infoVO = intef.seachTransInfo(vo,transType);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
	}
}
